<template>
  <div class="container">

    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in rowList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="setRoles">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { getRoles } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/PageTools'
export default {
  name: 'AssignRole',
  props: {
    userId: {
      type: String,
      required: true
    }

  },
  data() {
    return {
      checkList: [],
      rowList: []
    }
  },
  created() {
    // 调用获取角色列表数据的方法
    this.getRoles()
    // 调用获取员工已有角色的方法
    this.getUserDetailById()
  },
  methods: {
    // 分配角色
    async setRoles() {
      const res = await assignRoles({ id: this.userId, roleIds: this.checkList })
      this.$message.success(res.message)
      this.$emit('close')
    },
    // 获取员工已有角色的方法
    async getUserDetailById() {
      const res = await getUserDetailById(this.userId)
      console.log(res)
      this.checkList = res.data.roleIds === null ? [] : res.data.roleIds
    },
    // 获取角色列表数据的方法
    async  getRoles() {
      const res = await getRoles()
      this.rowList = res.data.rows
    },
    // 点击取消
    cancleDialog() {
      this.$emit('close')
    }
  }
}
</script>
<style scoped lang="scss">
.el-checkbox-group {
    font-size: 0;
    padding-bottom: 20px;
}
</style>
